import {Component} from '@angular/core';
import {NavController, AlertController} from 'ionic-angular';
import {Dialogs} from '@ionic-native/dialogs';
import {LoginPage} from "../login/login";
import {PwdService} from "./pwd.service";

@Component({
    selector: 'pwd',
    templateUrl: 'pwd.html',
    providers: [PwdService]
})
export class PwdPage {
    hasClick = false;
    errorTip = false;        //为true显示错误提示
    errorTipMsg = "";
    hq = "获取验证码";
    num = 60;

    constructor(public navCtrl: NavController,
                public dialogs: Dialogs,
                public alertCtrl: AlertController,
                public pwdService: PwdService) {

    }

    userInfo: UserInfo = {
        phone: '',
        yzm: '',
        newPassWord: '',
        rePassWord: ''
    }


    //获取验证码
    TofindPasswordCode() {
        console.log(this.userInfo.phone)
        if (!(/^1[34578]\d{9}$/.test(this.userInfo.phone))) {
            let alert = this.alertCtrl.create({
                title: '',
                subTitle: '输入手机号格式不正确，请重新输入',
                buttons: [
                    {
                        text: 'OK',
                        handler: data => {
                            this.userInfo.phone = '';
                        }
                    }
                ]
            });
            alert.present();
        } else {
            this.hasClick = true;
            let timer = setInterval(() => {
                if (this.num == 50) {
                    clearInterval(timer);
                    this.hasClick = false;
                    this.hq = "重新获取";
                    this.num = 60;
                    return;
                }
                this.num--;
            }, 1000)
            let param = {
                "mobile": this.userInfo.phone
            }
            this.pwdService.findPasswordCode(param)
                .then(data => {
                    console.log(data);
                    if (data.resultStatus == 'success') {

                    }

                }, err => {
                    console.log('错误信息', err);
                })
                .catch(fail => {
                    console.log('异常信息', fail);
                });
        }

    }


    goToHome() {
        this.navCtrl.setRoot(LoginPage);
    }

    //找回密码
    TofindPassword() {
        if (this.userInfo.yzm == null || this.userInfo.yzm == "") {
            this.errorTip = true;
            this.errorTipMsg = "提交失败，您输入的验证码有误！";
        } else if (this.userInfo.newPassWord != this.userInfo.rePassWord) {
            this.errorTip = true;
            this.errorTipMsg = "两次输入的密码不一致！";
        }
        else {
            this.errorTip = false;
            //调用接口
            console.log('调用修改密码接口');
            let param = {
                VerificationCode: this.userInfo.yzm,
                mobile: this.userInfo.phone,
                password: this.userInfo.newPassWord
            };
            this.pwdService.findPassword(param)
                .then(data => {
                    console.log(data);
                    if (data.resultStatus == 'success') {
                        let alert = this.alertCtrl.create({
                            title: '',
                            subTitle: '密码修改成功，请使用新密码登录!',
                            buttons: [
                                {
                                    text: 'OK',
                                    handler: data => {
                                        this.navCtrl.goToRoot(LoginPage);
                                    }
                                }
                            ]
                        });
                        alert.present();
                    }

                }, err => {
                    console.log('登陆错误', err);
                })
                .catch(fail => {
                    console.log('登录异常', fail);
                });
        }
    }
}
export class UserInfo {
    phone: string;
    yzm: string;
    newPassWord: string;
    rePassWord: string;
}
